import {
	doCodeLogin
} from "@/apis/user.js"
import {
	useAppsStore
} from '@/stores/modules/app.js'

const getCode = () => {
	return new Promise((rel, rej) => {
		uni.login({
			success(e) {
				rel(e.code)
			}
		})
	})
}

const autoLogin = async (token = '') => {
	if (token) {
		return store.dispatch('app/setToken', token)
		return
	}
	const codes = await getCode()
	const data = await doCodeLogin({
		code: codes,
	})
	if (data?.token) {
		const {
			setToken
		} = useAppsStore()
		setToken(data.token)
	}
}

export default {
	autoLogin
}